package com.hm.base.api;

import java.io.File;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.hm.base.auto.SystemEnvConfigProperties;
import com.hm.base.auto.SystemEnvConfigProperties.BackstageAccreditInfo;
import com.hm.base.auto.helper.HmSessionFactory;
import com.hm.base.auto.su.ControllerResult;
import com.hm.base.auto.su.R.Restful;
import com.hm.common.annotation.WebClientRequestIntercept;
import com.hm.common.oss.qiniu.su.QiNiuHandlerSupport;
import com.hm.common.su.bean.ServerResponse;
import com.hm.common.util.DateUtil;
import com.hm.common.util.FileUtil;
import com.hm.common.util.ServerUtil;
import com.hm.common.util.ServerUtil.ServerInfoVo;

import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;

/**
 * @author shishun.wang
 * @date 下午3:07:30 2017年6月16日
 * @version 1.0
 * @describe 公共组件api
 */
@Slf4j
@ResponseBody
@RestController
@RequestMapping(Restful.API + "/commmon")
public class CommonApi {

	private static final String SWWAGER_TAG = "公共服务管理";

	@Autowired
	private QiNiuHandlerSupport qiNiuHandlerSupport;

	@Autowired
	private SystemEnvConfigProperties systemEnvConfigProperties;

	@WebClientRequestIntercept(auth = false)
	@RequestMapping(value = "/heartbeat", method = RequestMethod.GET)
	@ApiOperation(tags = SWWAGER_TAG, value = "服务心跳远程监测,防止程序出现假死", httpMethod = "GET", notes = "com.hm.base.api.CommonApi::heartbeat")
	public ResponseEntity<ServerResponse<Boolean>> heartbeat() {
		log.debug(DateUtil.now4yyyyMMddhhmm() + "心跳检测");
		return ControllerResult.success(true);
	}

	@RequestMapping(value = "/backstage/accredit/info", method = RequestMethod.GET)
	@ApiOperation(tags = SWWAGER_TAG, value = "获取后台系统版本及授权基本信息", httpMethod = "GET", notes = "com.hm.base.api.CommonApi::loadBackstageAccreditInfo")
	public ResponseEntity<ServerResponse<BackstageAccreditInfo>> loadBackstageAccreditInfo() {
		log.debug("用户{},获取后台系统版本及授权基本信息", HmSessionFactory.currentUserId());
		return ControllerResult.success(systemEnvConfigProperties.getBackstageAccreditInfo());
	}

	@RequestMapping(value = "/env-serverinfo", method = RequestMethod.PATCH)
	@ApiOperation(tags = SWWAGER_TAG, value = "获取系统运行服务器环境", httpMethod = "PATCH", notes = "com.hm.base.api.CommonApi::envServerinfo")
	public ResponseEntity<ServerResponse<ServerInfoVo>> envServerinfo() {
		return ControllerResult.success(ServerUtil.baseInfo());
	}

	@RequestMapping(value = "/upload", method = RequestMethod.POST)
	@ApiOperation(tags = SWWAGER_TAG, value = "上传文件", httpMethod = "POST", notes = "com.hm.base.api.CommonApi::uploadFile")
	public ResponseEntity<ServerResponse<String>> uploadFile(@ApiParam(required = true, name = "file_data", value = "目标") @RequestParam("file_data") MultipartFile file) {
		try {
			String filename = file.getOriginalFilename();
			String suffix = filename.substring(filename.lastIndexOf(".") + 1, filename.length());
			File tempFile = File.createTempFile(filename, suffix);
			tempFile.deleteOnExit();
			file.transferTo(tempFile);
			byte[] datas = FileUtil.getFile(tempFile);
			String uploadFileUri = qiNiuHandlerSupport.buildSampleHandler().upload(System.nanoTime() + "." + suffix, datas);

			return ControllerResult.success(uploadFileUri);
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			return ControllerResult.failed(e.getMessage());
		}
	}

}
